package com.huihuiyun.huihuiyun.server.controller.seller.website;

import com.huihuiyun.huihuiyun.server.controller.seller.website.authorize.SellerWebsiteAuthorizeHandler;
import com.huihuiyun.huihuiyun.server.controller.seller.website.viewobjects.HhyPointsMallProductVO;
import com.huihuiyun.huihuiyun.server.domain.HhyProduct;
import com.huihuiyun.huihuiyun.server.domain.HhySeller;
import com.huihuiyun.huihuiyun.server.service.HhyProductService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path(Const.context)
@Component
@Slf4j
public class SellerWebsitePointsMallProductController {
    @Autowired
    private SellerWebsiteAuthorizeHandler sellerWebsiteAuthorizeHandler;

    @Autowired
    private HhyProductService hhyPointsMallProductService;

    @Context
    private HttpServletRequest httpServletRequest;

    @Context
    private HttpServletResponse httpServletResponse;

    /**
     * 是否有权限
     */
    private static boolean hasRight(HhySeller hhySeller, HhyProduct hhyPointsMallProduct) {
        return StringUtils.equals(hhyPointsMallProduct.sellerId, hhySeller.id);
    }

    @POST
    @Path("/pointsMallProduct/add")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO add(HhyPointsMallProductVO hhyPointsMallProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            // 业务
            HhyProduct hhyPointsMallProduct = ResultVOUtil.convert(hhyPointsMallProductVO, HhyProduct.class);
            hhyPointsMallProduct.sellerId = hhySeller.id;
            hhyPointsMallProduct = hhyPointsMallProductService.put(hhyPointsMallProduct);
            hhyPointsMallProductVO = ResultVOUtil.convert(hhyPointsMallProduct, HhyPointsMallProductVO.class);
            return ResultVOUtil.success(hhyPointsMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("创建失败，请稍后重试");
        }
    }

    @POST
    @Path("/pointsMallProduct/deleteOne")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteOne(HhyPointsMallProductVO hhyPointsMallProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String id = hhyPointsMallProductVO.id;
            HhyProduct hhyPointsMallProduct = hhyPointsMallProductService.findOne(id);
            if (!hasRight(hhySeller, hhyPointsMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            hhyPointsMallProductService.delete(id);
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/pointsMallProduct/deleteAll")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteAll(List<HhyPointsMallProductVO> hhyPointsMallProductVOList) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            for (HhyPointsMallProductVO hhyPointsMallProductVO : hhyPointsMallProductVOList) {
                String id = hhyPointsMallProductVO.id;
                HhyProduct hhyPointsMallProduct = hhyPointsMallProductService.findOne(id);
                if (!hasRight(hhySeller, hhyPointsMallProduct)) {
                    return ResultVOUtil.error("没有权限");
                }
            }

            List<HhyProduct> hhyPointsMallProductList = ResultVOUtil.convertList(hhyPointsMallProductVOList, HhyProduct.class);
            // 业务
            for (HhyProduct hhyPointsMallProduct : hhyPointsMallProductList) {
                hhyPointsMallProductService.delete(hhyPointsMallProduct.id);
            }
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/pointsMallProduct/update")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO updatePointsMallProduct(HhyPointsMallProductVO hhyPointsMallProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String id = hhyPointsMallProductVO.id;
            HhyProduct hhyPointsMallProduct = hhyPointsMallProductService.findOne(id);
            if (!hasRight(hhySeller, hhyPointsMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            hhyPointsMallProduct = ResultVOUtil.copyProperties(hhyPointsMallProductVO, hhyPointsMallProduct);
            hhyPointsMallProduct = hhyPointsMallProductService.put(hhyPointsMallProduct);
            hhyPointsMallProductVO = ResultVOUtil.convert(hhyPointsMallProduct, HhyPointsMallProductVO.class);
            return ResultVOUtil.success(hhyPointsMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("更新失败，请稍后重试");
        }
    }

    @GET
    @Path("/pointsMallProduct/findOne")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO<HhyPointsMallProductVO> findPointsMallProduct(@QueryParam("id") String id) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            HhyProduct hhyPointsMallProduct = hhyPointsMallProductService.findOne(id);
            if (!hasRight(hhySeller, hhyPointsMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            HhyPointsMallProductVO hhyPointsMallProductVO = ResultVOUtil.convert(hhyPointsMallProduct, HhyPointsMallProductVO.class);
            return ResultVOUtil.success(hhyPointsMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }

    @GET
    @Path("/pointsMallProduct/findAll")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO findAllPointsMallProduct(@QueryParam("currentPage") @DefaultValue("1") int currentPage, @QueryParam("pageSize") @DefaultValue("10") int pageSize) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            // 业务
            String sellerId = hhySeller.id;
            int mallType = HhyProduct.mallTypePointsMall;
            currentPage = ResultVOUtil.getCurrentPageFromAntDesignProQueryParam(currentPage);
            Page<HhyProduct> pointsMallProductPage = hhyPointsMallProductService.findAllBySellerIdAndMallType(sellerId, mallType, currentPage, pageSize);
            Object data = ResultVOUtil.convertJpaPage2AntDesignTableData(pointsMallProductPage, HhyPointsMallProductVO.class);
            return ResultVOUtil.success(data);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }


    @POST
    @Path("/pointsMallProduct/up")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO upProduct(HhyPointsMallProductVO hhyProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String productId = hhyProductVO.id;
            HhyProduct hhyPointsMallProduct = hhyPointsMallProductService.findOne(productId);
            if (!hasRight(hhySeller, hhyPointsMallProduct)) {
                return ResultVOUtil.error("不能上架其他商家的商品");
            }
            // 业务
            hhyPointsMallProduct.status = HhyProduct.statusUp;
            hhyPointsMallProduct = hhyPointsMallProductService.put(hhyPointsMallProduct);
            HhyPointsMallProductVO hhyProductVOOutput = ResultVOUtil.convert(hhyPointsMallProduct, HhyPointsMallProductVO.class);
            return ResultVOUtil.success(hhyProductVOOutput);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error(1, "创建失败，请稍后重试");
        }
    }

    @POST
    @Path("/pointsMallProduct/down")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO downProduct(HhyPointsMallProductVO hhyProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String productId = hhyProductVO.id;
            HhyProduct hhyPointsMallProduct = hhyPointsMallProductService.findOne(productId);
            if (!hasRight(hhySeller, hhyPointsMallProduct)) {
                return ResultVOUtil.error("不能下架其他商家的商品");
            }
            // 业务
            hhyPointsMallProduct.status = HhyProduct.statusDown;
            hhyPointsMallProduct = hhyPointsMallProductService.put(hhyPointsMallProduct);
            HhyPointsMallProductVO hhyProductVOOutput = ResultVOUtil.convert(hhyPointsMallProduct, HhyPointsMallProductVO.class);
            return ResultVOUtil.success(hhyProductVOOutput);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error(1, "创建失败，请稍后重试");
        }
    }

}
